﻿// -----------------------------------------------------------------------
// <copyright file="AgrupadorDeQuerysPorPatron.cs" company="Hexacta S.A.">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace parser
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using gudusoft.gsqlparser;
    using gudusoft.gsqlparser.Units;
    using generador;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AgrupadorDeQuerysPorPatron
    {
        ArrayList infos_parseadas_del_log;
        Dictionary<string, SQLPattern> diccionario_de_patrones = new Dictionary<string, SQLPattern>();

        public AgrupadorDeQuerysPorPatron(ArrayList _infos_parseadas_del_log )
        {
            infos_parseadas_del_log = _infos_parseadas_del_log;
        }

        public List<SQLPattern> Agrupar()
        {
            //AGRUPAR
            foreach (InfoDeLaQuery info in infos_parseadas_del_log)
            {
                PatternKeyStructure patron_key = ObtenerPatron(info);
                string patron_key_serializada = patron_key.Serializar();

                SQLPattern el_patron;
                if (diccionario_de_patrones.ContainsKey(patron_key_serializada))
                {
                    el_patron = diccionario_de_patrones[patron_key_serializada];
                    el_patron.AgregarNuevaQuery(info);
                }
                else 
                {
                    if (patron_key.Es_delete)
                    {
                        //hago esta distincion porque este patron no tiene subagrupacion de queryes en subpatrones
                        el_patron = new SQLDeletePattern(); 
                    }
                    else
                    {
                        el_patron = new SQLPattern();
                    }
                    el_patron.AgregarNuevaQuery(info);
                    el_patron.Tablas = patron_key.Tablas;
                    el_patron.PatternName = patron_key_serializada;
                    diccionario_de_patrones.Add(patron_key_serializada, el_patron);
                }
            }
            return diccionario_de_patrones.Values.ToList();
        }

        
        
        private PatternKeyStructure ObtenerPatron(InfoDeLaQuery info)
        {
            PatternKeyStructure patron = new PatternKeyStructure();
            
            patron.Es_select = info.Query.SqlStatementType == TSqlStatementType.sstSelect;
            patron.Es_update = info.Query.SqlStatementType == TSqlStatementType.sstUpdate;
            patron.Es_delete = info.Query.SqlStatementType == TSqlStatementType.sstDelete;
            patron.Es_insert = info.Query.SqlStatementType == TSqlStatementType.sstInsert;
            patron.Es_tiene_where=info.Query.WhereClause != null;
            

            switch (info.Query.SqlStatementType)
            {
                case TSqlStatementType.sstSelect:
                    TSelectSqlStatement selectstmt = (TSelectSqlStatement) info.Query;
                    patron.Es_tiene_group_by = selectstmt.GroupbyClause != null;
                    break;

                case TSqlStatementType.sstUpdate:
                    TUpdateSqlStatement updatestmt = (TUpdateSqlStatement) info.Query;
                    patron.Es_tiene_group_by = false;
                    break;
                 case TSqlStatementType.sstDelete:
                    TDeleteSqlStatement deletestmt = (TDeleteSqlStatement) info.Query;
                    patron.Es_tiene_group_by = false;
                    break;
                 case TSqlStatementType.sstInsert:
                    TInsertSqlStatement insertstmt = (TInsertSqlStatement) info.Query;
                    patron.Es_tiene_group_by = false;
                    break;
            }

            foreach (TLzTable table in info.Query.Tables)
	        {
		       patron.Tablas.Add( table.TableName);
	        }

            return patron;
        }
    }
}
